function fun_back() {
    var back_url = GetQueryString("back_url");
    if(back_url!=null && back_url!='' && back_url!='null'){
       window.location.href =decodeURIComponent(back_url);
    }else {
        window.history.go(-1);
    }

}
function fun_enter() {
    if (focus == 1) {//上一页 下一页
        if (result == "pre") {
            currPage--;
        } else if (result == "next") {
            currPage++;
        }
        focus = 0;
        currContent = 1;
        showProductList();
    } else if (focus == 2) {//左边内容排序或筛选条件
        addClassToSort(select);
        var sort = getSortSelect();
        currContent = 1;
        currPage = 1;
        currData = getSortData(sort);
        showProductList();
    } else if (focus == 0) {//点击内容展示详细页面
        var infoObj = parseInt(currContent) + (parseInt(currPage) - 1) * 6;
        var id =currData[parseInt(infoObj) - 1].id;
        var local = window.location.href;
        var str = '';
        var index_url = GetQueryString("back_url");
        if(index_url!=null && index_url!='' && index_url!='null'){
            str ='&index_url='+encodeURIComponent(index_url);
        }
        window.location = 'insurance.html?id='+id+'&back_url='+encodeURIComponent(local)+str;
    }
}

//获取排序
function getSortSelect() {
    var len =  $("sort").children.length;
    for (var i = 1; i <= len; i++) {
        var obj = document.getElementById("sort-" + i);
        if (obj != null) {
            if (obj.getAttribute('class')=='selectNormal') {
                return i;
            }
        }

    }
}



function getSortData(sortNum) {
    //排序
    if(sortNum==1){
        currData = currData.sort(descend("id"));
    }else if(sortNum==2){
        currData = currData.sort(descend("money"));
    }else if(sortNum==3){
        currData = currData.sort(compare("sell"))
    }
    return currData;
}


function compare(property) {
    return function (a, b) {
        var value1 = parseFloat(a[property]);
        var value2 = parseFloat(b[property]);
        if (value2 < value1) {
            return -1;
        }
        else if (value2 > value1) {
            return 1;
        }
        else {
            return 0;
        }
    }
}

function descend(property) {
    return function (a, b) {
        var value1 = parseFloat(a[property]);
        var value2 = parseFloat(b[property]);
        if (value2 < value1) {
            return 1;
        }
        else if (value2 > value1) {
            return -1;
        }
        else {
            return 0;
        }
    }
}

function returnLevel(time) {
    time = time / 30;
    if (time > 0 && time <= 6) {
        return 2;
    } else if (time > 6 && time <= 12) {
        return 3;
    } else if (time > 12) {
        return 4;
    }
}




function addClassToSort(num) {
    var len = $("sort").children.length;
    var id = "sort-";
    var number = num;
    if (num > len) {
        len = 4;
        number = num - 2;
        id = "limitTime-";
    }
    for (var i = 1; i < len + 1; i++) {
        var obj = document.getElementById(id + i);
        if (obj != null) {
            if (obj.getAttribute('class')=='selectNormal') {
                removeClass("selectNormal", obj);
            }
        }

    }
    var selectObj = document.getElementById(id + "" + number);
    if (selectObj != null) {
        selectObj.className = 'selectNormal'
    }

}


function fun_right() {
    if (focus == 0) {
        content = document.getElementById("content-" + currContent);
        var contents = getNearPos(content, all_div, "right");
        if (contents != null) {
            content = contents;
            currContent = content.id.split("-")[1];
            addBorder(currContent);
        }
    } else if (focus == 1) {
        if (currPage != 1 && currPage < total) {//中间页面
            result = "next";
            addResultFocus(1);
        }

    } else if (focus == 2) {
        focus = 0;
        addBorder(currContent);
    }
}

function fun_left() {
    if (focus == 0) {
        /*if(currContent%2==0){
         currContent--;
         addBorder(currContent);
         }*/
        content = document.getElementById("content-" + currContent);
        var contents = getNearPos(content, all_div, "left");
        if (contents != null) {
            content = contents;
            currContent = content.id.split("-")[1];
            addBorder(currContent);
        } else if (contents == null) {
            if (currContent % 2 == 0) {
                currContent--;
                addBorder(currContent);
                content = document.getElementById("content-" + currContent)
            } else if (currContent % 2 == 1) {
                focus = 2;
                select = 1;
                addSelectFocus(select);
            }
        }
    } else if (focus == 1) {
        if (currPage != 1 && currPage < total) {//中间页面
            result = "pre";
            addResultFocus(0);
        }

    }
}

function fun_up() {
    if (focus == 0) {
        /*if(currContent>2){
         currContent=currContent-2;
         addBorder(currContent);
         }*/
        content = document.getElementById("content-" + currContent);
        var contents = getNearPos(content, all_div, "up");
        if (contents != null) {
            content = contents;
            currContent = content.id.split("-")[1];
            addBorder(currContent);
        }
    } else if (focus == 1) {
        focus = 0;
        addBorder(currContent);
    } else if (focus == 2) {
        if (select > 1) {
            select--;
            addSelectFocus(select);
        }

    }
}
function fun_down() {
    if (focus == 0) {
        content = document.getElementById("content-" + currContent);
        var contents = getNearPos(content, all_div, "down");
        if (contents != null) {
            content = contents;
            currContent = content.id.split("-")[1];
            addBorder(currContent);
        } else if (contents == null) {
            if (total > 1) {
                if (currPage == 1) {//第一页
                    result = "next";
                } else {//中间页 或者 最后一页
                    result = "pre";
                }
                //result =  document.getElementById("result-1");
                addResultFocus(0);
                focus = 1
            }
        }
    } else if (focus == 2) {
        var size = 6;
        if (type == 2) {
            size = 3;
        }
        if (select < size) {
            select++;
            addSelectFocus(select);
        }

    }
}